<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */


class UserFollowModel extends Model
{

	/**
	 * 增加关注
	 *
	 * @param int $sourceID
	 * @param int $targetID
	 * @return null
	 */
	public function addFollow($sourceID, $targetID)
	{
	    $User = D('User');

	    $data = array(
	        'source_user' => $sourceID,
	        'target_user' => $targetID,
	        'create_time' => time(),
	        'status' => 1,
	    );
	    $this->replace($data);

	    $map = array(
	        'source_user' => $sourceID,
	        'status' => 1,
	    );
	    $followingCount = $this->where($map)->count();
	    $where = array('user_id' => $sourceID);
	    $User->where($where)->setField('following_count', $followingCount);

	    $map = array(
	        'target_user' => $targetID,
	        'status' => 1,
	    );
	    $followerCount = $this->where($map)->count();
	    $where = array('user_id' => $targetID);
	    $User->where($where)->setField('follower_count', $followerCount);
	}


	/**
	 * 移除粉丝
	 * @param int $sourceID
	 * @param int $targetID
	 * @return null
	 */
	public function rmFollow($sourceID, $targetID)
	{
	    $User = D('User');

	    $where = array(
	        'source_user' => $sourceID,
	        'target_user' => $targetID,
	        'status' => 1,
	    );
	    $this->where($where)->delete();

	    $map = array(
	        'source_user' => $sourceID,
	        'status' => 1,
	    );
	    $followingCount = $this->where($map)->count();
	    $where = array('user_id' => $sourceID);
	    $User->where($where)->setField('following_count', $followingCount);

	    $map = array(
	        'target_user' => $targetID,
	        'status' => 1,
	    );
	    $followerCount = $this->where($map)->count();
	    $where = array('user_id' => $targetID);
	    $User->where($where)->setField('follower_count', $followerCount);
	}


	/**
	 * 封锁关注
	 *
	 * @param int $sourceID
	 * @param int $targetID
	 * @return null
	 */
	public function addBlock($sourceID, $targetID)
	{
	    $User = D('User');

	    $data = array(
	        'source_user' => $sourceID,
	        'target_user' => $targetID,
	        'status' => -1,
	    );
	    $this->replace($data);

	    $where = array(
	       'source_user' => $targetID,
	       'target_user' => $sourceID,
	    );
	    $this->where($where)->delete();

	    $map = array(
	        'source_user' => $sourceID,
	        'status' => 1,
	    );
	    $followingCount = $this->where($map)->count();
	    $where = array('user_id' => $sourceID);
	    $User->where($where)->setField('following_count', $followingCount);

	    $map = array(
	        'target_user' => $targetID,
	        'status' => 1,
	    );
	    $followerCount = $this->where($map)->count();
	    $where = array('user_id' => $targetID);
	    $User->where($where)->setField('follower_count', $followerCount);
	}


	/**
	 * 解除封锁
	 *
	 * @param int $sourceID
	 * @param int $targetID
	 * @return null
	 */
	public function rmBlock($sourceID, $targetID)
	{
	    $where = array(
	        'source_user' => $sourceID,
	        'target_user' => $targetID,
	        'status' => -1,
	    );
	    $this->where($where)->delete();
	}
	
	
	/**
	 * 检查关注的问会员数是否超出限制('ture' => '超出', 'false' => '未超出')
	 *
	 * @param int $userID
	 * @param int $userGroup
	 * @return  bool 
	 */
	public function checkFollowLimit($userID, $userGroup)
	{
		$userGroup = F('cache_usergroup_'. $userGroup);
        $map = array(
        	'source_user' => $userID,
        	'status' => 1,
        );
        $followCount = $this->where($map)->count();
        return ($followCount >= $userGroup['max_user_follow']) ? false : true;
	}

}

/* End of file UserFollowModel.class.php */
/* Location: ./App/Lib/Model/UserFollowModel.class.php */